﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using appDataContracts;
using appClient;
using appInterfaces;
using System.ServiceModel;

namespace appClient
{
	public partial class MyOrders : System.Web.UI.Page
	{
		IClient client;
		int orderDetailsID;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (Session["user"] != null)
			{
				//fillOrdersInProgress();
				//fillOrdersDone();
				client = new ChannelFactory<IClient>("clientHttp").CreateChannel();

				if (Request.QueryString["id"] != null)
				{
					orderDetailsID = int.Parse(Request.QueryString["id"]);
					fillOrderDetails();
				}
				else
					fillOrders();
			}
			else
			{
				Response.Redirect("Default.aspx");
			}
		}


		public void fillOrders()
		{
			List<SerializedOrder> ordersNotDone = new List<SerializedOrder>();
			List<SerializedOrder> ordersDone = new List<SerializedOrder>();
			ordersNotDone = client.getOrders(false, ((SerializedClient)Session["user"]).CLIENTID);
			ordersDone = client.getOrders(true, ((SerializedClient)Session["user"]).CLIENTID);

			List<List<SerializedOrder>> orders = new List<List<SerializedOrder>>();
			orders.Add(ordersNotDone);
			orders.Add(ordersDone);

			bool listingOrdersNotDone = true;


			// filling table
			foreach (List<SerializedOrder> sOrderList in orders)
			{
				HtmlGenericControl boxDiv = new HtmlGenericControl("div");
				HtmlGenericControl ltDiv = new HtmlGenericControl("div");
				HtmlGenericControl rtDiv = new HtmlGenericControl("div");
				HtmlGenericControl rbDiv = new HtmlGenericControl("div");
				HtmlGenericControl lbDiv = new HtmlGenericControl("div");
				HtmlGenericControl innerDiv = new HtmlGenericControl("div");
				HtmlGenericControl wrapperDiv = new HtmlGenericControl("div");

				HtmlGenericControl h3 = new HtmlGenericControl("h3");

				HtmlGenericControl table = new HtmlGenericControl("table");
				HtmlGenericControl thead = new HtmlGenericControl("thead");
				HtmlGenericControl tr = new HtmlGenericControl("tr");
				HtmlGenericControl td1 = new HtmlGenericControl("td");
				HtmlGenericControl td2 = new HtmlGenericControl("td");
				HtmlGenericControl td3 = new HtmlGenericControl("td");
				HtmlGenericControl td4 = new HtmlGenericControl("td");
				HtmlGenericControl tbody = new HtmlGenericControl("tbody");

				foreach (SerializedOrder sOrder in sOrderList)
				{
						boxDiv.Attributes.Add("class", "box");
						ltDiv.Attributes.Add("class", "left-top-corner");
						rtDiv.Attributes.Add("class", "right-top-corner");
						rbDiv.Attributes.Add("class", "right-bot-corner");
						lbDiv.Attributes.Add("class", "left-bot-corner");
						wrapperDiv.Attributes.Add("class", "wrapper line-ver");
						td1.Attributes.Add("class", "tdHeader");
						td2.Attributes.Add("class", "tdHeader");
						td3.Attributes.Add("class", "tdHeader");
						td4.Attributes.Add("class", "tdHeader");
						if(listingOrdersNotDone)
							tbody.Attributes.Add("class", "tbodyOrdersInProgress");
						else
							tbody.Attributes.Add("class", "tbodyOrdersDone");
						tbody.Attributes.Add("runat", "server");

						td1.InnerText = "N°";
						td2.InnerText = "Date";
						td3.InnerText = "Payé";
						td4.InnerText = "Total";

						if (listingOrdersNotDone)
							h3.InnerText = "En cours";
						else
							h3.InnerText = " Passées";

						tr.Controls.Add(td1);
						tr.Controls.Add(td2);
						tr.Controls.Add(td3);
						tr.Controls.Add(td4);
						thead.Controls.Add(tr);
						table.Controls.Add(thead);
						table.Controls.Add(tbody);
						wrapperDiv.Controls.Add(h3);
						wrapperDiv.Controls.Add(table);
						innerDiv.Controls.Add(wrapperDiv);
						lbDiv.Controls.Add(innerDiv);
						rbDiv.Controls.Add(lbDiv);
						rtDiv.Controls.Add(rbDiv);
						ltDiv.Controls.Add(rtDiv);
						boxDiv.Controls.Add(ltDiv);

					HtmlGenericControl tableRow = new HtmlGenericControl("tr");

					HtmlGenericControl orderIdCell = new HtmlGenericControl("td");
					HtmlGenericControl orderDateCell = new HtmlGenericControl("td");
					HtmlGenericControl orderPaidCell = new HtmlGenericControl("td");
					HtmlGenericControl orderTotalCell = new HtmlGenericControl("td");
					HtmlGenericControl seeMoreCell = new HtmlGenericControl("td");

					orderIdCell.InnerText = sOrder.ORDERID.ToString();
					orderDateCell.InnerText = sOrder.ORDERDATE.ToString();
					if(sOrder.ORDERPAID)
						orderPaidCell.InnerHtml = "<img src=\"Styles/images/v.png\" />";
					else
						orderPaidCell.InnerHtml = "<img src=\"Styles/images/x.png\" />";
					orderTotalCell.InnerText = sOrder.ORDERTOTAL.ToString()+" €";
					seeMoreCell.InnerHtml = "<a href=\"MyOrders.aspx?id=" + sOrder.ORDERID.ToString() + "\" class=\"button\">Détails</a>";

					tableRow.Controls.Add(orderIdCell);
					tableRow.Controls.Add(orderDateCell);
					tableRow.Controls.Add(orderPaidCell);
					tableRow.Controls.Add(orderTotalCell);
					tableRow.Controls.Add(seeMoreCell);

					if (listingOrdersNotDone == true)
					{
						//tbodyOrdersInProgress.Controls.Add(tableRow);
						tbody.Controls.Add(tableRow);
						sectionS.Controls.Add(boxDiv);
					}
					else
					{
						//tbodyOrdersDone.Controls.Add(tableRow);
						tbody.Controls.Add(tableRow);
						sectionS.Controls.Add(boxDiv);
					}
				}

				listingOrdersNotDone = false;
			}
		}



		// TO DO !!!
		public void fillOrderDetails()
		{
			SerializedOrder order = client.getOrderDetails(orderDetailsID);

			#region htmlcontrols

			HtmlGenericControl boxDiv = new HtmlGenericControl("div");
			HtmlGenericControl ltDiv = new HtmlGenericControl("div");
			HtmlGenericControl rtDiv = new HtmlGenericControl("div");
			HtmlGenericControl rbDiv = new HtmlGenericControl("div");
			HtmlGenericControl lbDiv = new HtmlGenericControl("div");
			HtmlGenericControl innerDiv = new HtmlGenericControl("div");
			HtmlGenericControl wrapperDiv = new HtmlGenericControl("div");
			HtmlGenericControl col1Div = new HtmlGenericControl("div");
			HtmlGenericControl h3 = new HtmlGenericControl("h3");

			HtmlGenericControl table = new HtmlGenericControl("table");
			HtmlGenericControl thead = new HtmlGenericControl("thead");
			HtmlGenericControl tr = new HtmlGenericControl("tr");
			HtmlGenericControl td1 = new HtmlGenericControl("td");
			HtmlGenericControl td2 = new HtmlGenericControl("td");
			HtmlGenericControl td3 = new HtmlGenericControl("td");
			HtmlGenericControl td4 = new HtmlGenericControl("td");
			HtmlGenericControl tbody = new HtmlGenericControl("tbody");

			boxDiv.Attributes.Add("class", "box");
			ltDiv.Attributes.Add("class", "left-top-corner");
			rtDiv.Attributes.Add("class", "right-top-corner");
			rbDiv.Attributes.Add("class", "right-bot-corner");
			lbDiv.Attributes.Add("class", "left-bot-corner");
			wrapperDiv.Attributes.Add("class", "wrapper line-ver");
			col1Div.Attributes.Add("class", "col-1");
			td1.Attributes.Add("class", "tdHeader");
			td2.Attributes.Add("class", "tdHeader");
			td3.Attributes.Add("class", "tdHeader");
			td4.Attributes.Add("class", "tdHeader");
			tbody.Attributes.Add("class", "tbodyOrderDetails");
			tbody.Attributes.Add("runat", "server");

			td1.InnerText = "Produit";
			td2.InnerText = "Prix unit.";
			td3.InnerText = "Quantité";
			td4.InnerText = "Total";


			h3.InnerText = " Détails de la commande N°" +orderDetailsID;
			
			#endregion


			foreach (SerializedContain article in order.ARTICLE)
			{
				HtmlGenericControl tableRow = new HtmlGenericControl("tr");

				HtmlGenericControl productCell = new HtmlGenericControl("td");
				HtmlGenericControl unitPriceCell = new HtmlGenericControl("td");
				HtmlGenericControl QuantityCell = new HtmlGenericControl("td");
				HtmlGenericControl orderTotalCell = new HtmlGenericControl("td");

				productCell.InnerText = client.getProductInfo(article.PRODUCTID).PRODUCTNAME;
				unitPriceCell.InnerText = client.getProductInfo(article.PRODUCTID).PRODUCTPRICE.ToString();
				QuantityCell.InnerText = article.CONTAINQUANTITY.ToString();
				decimal totalBill = article.CONTAINQUANTITY * client.getProductInfo(article.PRODUCTID).PRODUCTPRICE;
				orderTotalCell.InnerText = totalBill.ToString() + " €";

				tableRow.Controls.Add(productCell);
				tableRow.Controls.Add(unitPriceCell);
				tableRow.Controls.Add(QuantityCell);
				tableRow.Controls.Add(orderTotalCell);

				tbody.Controls.Add(tableRow);
			}

			tr.Controls.Add(td1);
			tr.Controls.Add(td2);
			tr.Controls.Add(td3);
			tr.Controls.Add(td4);
			thead.Controls.Add(tr);
			table.Controls.Add(thead);
			table.Controls.Add(tbody);
			wrapperDiv.Controls.Add(h3);
			wrapperDiv.Controls.Add(table);
			innerDiv.Controls.Add(wrapperDiv);
			lbDiv.Controls.Add(innerDiv);
			rbDiv.Controls.Add(lbDiv);
			rtDiv.Controls.Add(rbDiv);
			ltDiv.Controls.Add(rtDiv);
			boxDiv.Controls.Add(ltDiv);

			sectionS.Controls.Add(boxDiv);
		}

	
	}
}